package com.qf.lanzhou.shop.api.exceptions;


import com.alibaba.fastjson.JSON;
import com.qf.lanzhou.shop.pojo.vo.BaseRespVO;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * @author 是柠檬甜
 * @date 2021/1/9 10:33
 */
@ControllerAdvice
public class ExceptionsHandler {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public BaseRespVO handlerException(Exception e) {
        BaseRespVO result = new BaseRespVO();
        result.setCode(BaseRespVO.CodeType.FAILED.getValue());
        result.setMsg(e.getMessage());
        return result;
    }

    @ExceptionHandler(value = BaseExceptions.class)
    @ResponseBody
    public String handlerUnLoginException(BaseExceptions e, HttpServletResponse resp) {
        BaseRespVO result = new BaseRespVO();
        result.setCode(e.getErrorCode());
        result.setMsg(e.getMessage());
        String str = JSON.toJSONString(result);
        return str;
    }
}
